package cs422.group1.projectsquirrel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.os.Environment;

public class ObservationDataManager {
	private static ObservationData data;
	 static boolean editObservation = false;
	
	public static ObservationData getData() 
	{ 
		if (data == null)
			data = new ObservationData();
		
		return data; 
	
	};
	
	public static boolean isEdit()
	{
		return editObservation;
	}
	
	public static boolean loadObservation(ObservationData observation)
	{
		data = observation;
		editObservation = true;
		return true;
	}
	
	public static ObservationData loadData(String observation)
	{
		ObservationData data = new ObservationData();
		StringBuffer buffer = new StringBuffer();
		File fdata = new File(observation);
		try {
			FileInputStream fIn = new FileInputStream(fdata);
			int tmp;
			while ((tmp = fIn.read()) != -1)
				buffer.append((char)tmp);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String[] arr = buffer.toString().split("\t");
	try
	{
		  data.observationDate = arr[0];
		  data.observationTime = arr[1];
		  data.foxSquirrels= arr[2];
		  data.graySquirrels= arr[3];
		  data.image= arr[4];
		  data.latitude= arr[5];
		  data.longitude= arr[6];
		  data.emailAddress= arr[7];
		  data.setting= arr[8];
		  data.nut_trees= arr[9];
		  data.seed_trees= arr[10];
		  data.fruit_trees= arr[11];
		  data.tiny_trees= arr[12];
		  data.con_trees= arr[13];
		  data.other_trees= arr[14];
		  data.feed_bird_feeder= arr[15];
		  data.feed_handouts= arr[16];
		  data.feed_garbage= arr[17];
		  data.feed_trees= arr[18];
		  data.feed_other= arr[19];
		  data.feed_other_details= arr[20];
		  data.num_squirrel_change= arr[21];
		  data.this_new_site= arr[22];
		  data.used_different_site= arr[23];
		  data.comments= arr[24];
		  data.site_dogs= arr[25];
		  data.site_cats= arr[26];
		  data.site_coyotes= arr[27];
		  data.site_hawks= arr[28];
		  data.site_grain= arr[29];
		  data.shortSurvey = Boolean.parseBoolean(arr[30]);
		  data.longSurvey = Boolean.parseBoolean(arr[31]);
		  data.csvFileName = arr[32];
	}
	catch (Exception e)
	{
		return null;
	}
		
		
     return data;
	}
	
	public static ArrayList<ObservationData> loadObservations()
	{
		ArrayList<ObservationData> observations = new ArrayList<ObservationData>();
		
		for (String observation : getFileList())
		{
			ObservationData tmp = loadData(observation);
			if (tmp != null)
				observations.add(tmp);
		}
		
		return observations;
	}
	
	public static ArrayList<String> getFileList()
	{
		ArrayList<String> fileListing = new ArrayList<String>();
		final File path = new File( Environment.getExternalStorageDirectory(), ProjectSquirrel.myAppContext.getPackageName() );
		File[] fileList = path.listFiles();
		for (File f : fileList )
		{
			if (f.getName().endsWith(".csv"))
				fileListing.add(f.getAbsolutePath());
		}
				
		return fileListing;
	}
	
	public static boolean saveData(ObservationData data)
	{
		final File path = new File( Environment.getExternalStorageDirectory(), ProjectSquirrel.myAppContext.getPackageName() );
		  if(!path.exists())
		  {
		    path.mkdir();
		  }
		  final String fileName = String.valueOf(new Date().hashCode()) + ".csv";
		  StringBuilder csv = new StringBuilder();
		  
		  csv.append(data.observationDate);
		  csv.append("\t");
		  csv.append(data.observationTime);
		  csv.append("\t");
		  csv.append(data.foxSquirrels);
		  csv.append("\t");
		  csv.append(data.graySquirrels);
		  csv.append("\t");
		  csv.append(data.image);
		  csv.append("\t");
		  csv.append(data.latitude);
		  csv.append("\t");
		  csv.append(data.longitude);
		  csv.append("\t");
		  csv.append(data.emailAddress);
		  csv.append("\t");
		  csv.append(data.setting);
		  csv.append("\t");
		  csv.append(data.nut_trees);
		  csv.append("\t");
		  csv.append(data.seed_trees);
		  csv.append("\t");
		  csv.append(data.fruit_trees);
		  csv.append("\t");
		  csv.append(data.tiny_trees);
		  csv.append("\t");
		  csv.append(data.con_trees);
		  csv.append("\t");
		  csv.append(data.other_trees);
		  csv.append("\t");
		  csv.append(data.feed_bird_feeder);
		  csv.append("\t");
		  csv.append(data.feed_handouts);
		  csv.append("\t");
		  csv.append(data.feed_garbage);
		  csv.append("\t");
		  csv.append(data.feed_trees);
		  csv.append("\t");
		  csv.append(data.feed_other);
		  csv.append("\t");
		  csv.append(data.feed_other_details);
		  csv.append("\t");
		  csv.append(data.num_squirrel_change);
		  csv.append("\t");
		  csv.append(data.this_new_site);
		  csv.append("\t");
		  csv.append(data.used_different_site);
		  csv.append("\t");
		  csv.append(data.comments);
		  csv.append("\t");
		  csv.append(data.site_dogs);
		  csv.append("\t");
		  csv.append(data.site_cats);
		  csv.append("\t");
		  csv.append(data.site_coyotes);
		  csv.append("\t");
		  csv.append(data.site_hawks);
		  csv.append("\t");
		  csv.append(data.site_grain);
		  csv.append("\t");
		  csv.append(data.shortSurvey);
		  csv.append("\t");
		  csv.append(data.longSurvey);
		  csv.append("\t");
		  csv.append(fileName);
		  
		  File newFile = null;
		  if (data.csvFileName != "")
		  {
			  newFile = new File(path, data.csvFileName);
		  }
		  else
			  newFile = new File(path, fileName);
		  try {
			FileOutputStream fOut = new FileOutputStream(newFile, false);
			fOut.write(csv.toString().getBytes());
			fOut.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
		  
		  return true;
	}
}